package com.fy.utilsproject.anim;

import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;

public class AnimationUtil {

    private static final String TAG = "AnimationUtil";

    /**
     * 通过动画改变控件宽高
     *
     * @param view       要操作的控件
     * @param isWidth    是否改变宽度，true改变宽度，false改变高度
     * @param startValue 初始值
     * @param endValue   结束值
     */
    public static void changeSizeWithAnimation(final View view, final boolean isWidth, int startValue, int endValue) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(startValue, endValue);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (isWidth) {
                    view.getLayoutParams().width = (int) animation.getAnimatedValue();
                } else {
                    view.getLayoutParams().height = (int) animation.getAnimatedValue();
                }
                view.requestLayout();
            }
        });
        valueAnimator.setDuration(500);
        valueAnimator.start();
    }

    public void cancelValueAnimator() {
        if (valueAnimator != null) {
            valueAnimator.cancel();
        }
    }

    private ValueAnimator valueAnimator;

    /**
     * 水平平移动画
     *
     * @param view             需要平移的控件
     * @param duration         动画时间，单位为毫秒
     * @param startX           平移的起始位置
     * @param endX             平移的终点位置
     * @param animatorListener 动画监听
     */
    public void translationByValueAnimator(final View view, long duration, final float startX, final float endX, final ValueAnimator.AnimatorListener animatorListener) {

        valueAnimator = ValueAnimator.ofFloat(startX, endX);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float x = (Float) animation.getAnimatedValue();
                view.setX(x);
            }
        });
        if (animatorListener != null)
            valueAnimator.addListener(animatorListener);
        valueAnimator.setDuration(duration);
        valueAnimator.start();
    }

    public static void translationVerticalByValueAnimator(final View view, long duration, final float startY, final float endY, final ValueAnimator.AnimatorListener animatorListener) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(startY, endY);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float y = (Float) animation.getAnimatedValue();
                Log.d(TAG, "translationVerticalByValueAnimator->change y:" + y);
                view.setY(y);
            }
        });
        if (animatorListener != null)
            valueAnimator.addListener(animatorListener);
        valueAnimator.setDuration(duration);
        valueAnimator.start();
    }

    /**
     * 以控件的中心为旋转中心进行旋转
     *
     * @param view        要旋转的控件
     * @param fromDegrees 起始角度
     * @param toDegrees   结束角度
     */
    public static void rotateView(View view, float fromDegrees, float toDegrees) {
        RotateAnimation rotateAnimation;
        rotateAnimation = new RotateAnimation(fromDegrees, toDegrees, view.getWidth() / 2f, view.getHeight() / 2f);
        rotateAnimation.setDuration(500);
        rotateAnimation.setFillAfter(true);
        view.startAnimation(rotateAnimation);
    }

    public static void rotateCenterView(View view, long duration) {
        //用0-360的话在终点会有停顿现象
        RotateAnimation rotateAnimation = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(duration);
        //匀速旋转
        rotateAnimation.setInterpolator(new LinearInterpolator());
        //无限循环
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        view.startAnimation(rotateAnimation);
    }
}
